import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import viteCompression from "vite-plugin-compression";
import postcsspxtoviewport from "postcss-px-to-viewport";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { join } from "path";
export default defineConfig({
    plugins: [
        vue(),
        vueJsx(),
        AutoImport({
            resolvers: [
                ElementPlusResolver({
                    importStyle: "sass"
                })
            ]
        }),
        Components({
            resolvers: [
                ElementPlusResolver({
                    importStyle: "sass"
                })
            ]
        }),
        viteCompression({
            verbose: true,
            disable: false,
            deleteOriginFile: false,
            threshold: 999, //压缩前最小文件大小
            algorithm: "gzip",
            ext: ".gz"
        })
    ],
    resolve: {
        alias: {
            "@": join(__dirname, "src")
        }
    },
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@use "@/assets/css/element.scss" as *;`
            },
            less: {
                javascriptEnabled: true,
                additionalData: `@import "@/assets/css/constant.less";`
            }
        },
        postcss: {
            plugins: [
                postcsspxtoviewport({
                    unitToConvert: "px", // 要转化的单位
                    viewportWidth: 1440, // UI设计稿的宽度
                    unitPrecision: 6, // 转换后的精度，即小数点位数
                    propList: ["*"], // 指定转换的css属性的单位，*代表全部css属性的单位都进行转换
                    viewportUnit: "vw", // 指定需要转换成的视窗单位，默认vw
                    fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位，默认vw
                    selectorBlackList: ["resert"], // 指定不转换为视窗单位的类名，
                    minPixelValue: 1, // 默认值1，小于或等于1px则不进行转换
                    mediaQuery: true, // 是否在媒体查询的css代码中也进行转换，默认false
                    replace: true, // 是否转换后直接更换属性值
                    // exclude: [/node_modules/], // 设置忽略文件，用正则做目录名匹配
                    include: [],
                    landscape: false // 是否处理横屏情况
                })
            ]
        }
    },
    build: {
        minify: "terser",
        // terserOptions: {
        //     compress: {
        //         drop_console: true,
        //         drop_debugger: true
        //     }
        // },
        rollupOptions: {
            output: {
                manualChunks(id) {
                    if (id.includes("node_modules")) {
                        return id.toString().split("node_modules/")[1].split("/")[0].toString();
                    }
                }
            }
        }
    },
    esbuild: { charset: "ascii" },
    server: {
        host: "0.0.0.0",
        proxy: {
            "/api": {
                target: "http://localhost:9008/gcdj",
                changeOrigin: true,
                ws: true,
                rewrite: (path) => path.replace(/^\/api/, "")
            },
            "/productionApi": {
                target: "http://192.168.1.131:9006/gcdj",
                changeOrigin: true,
                ws: true,
                rewrite: (path) => path.replace(/^\/productionApi/, "")
            }
        },
        cors: true
    }
});
